function returnIndex(){
    window.location.href = 'index.html';
}

document.addEventListener('DOMContentLoaded', function() {
    const loginForm = document.getElementById("loginForm");
    if (loginForm) {
        loginForm.addEventListener("submit", function(event) {
            event.preventDefault();  //阻止表单的默认提交行为（即阻止页面刷新）。

            const formData = new FormData(this);
            const formBody = new URLSearchParams(formData);

            fetch("http://localhost:8089/api/admin/login", {
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                body: formBody
            })
                .then(res => res.text())
                .then(result => {
                    if (result === "登录成功") {
                        alert("登录成功！");
                        window.location.href = "index.html"; // 跳转主页或管理页
                    } else {
                        alert("登录失败：" + result);
                    }
                })
                .catch(error => {
                    console.error("登录请求错误：", error);
                });
        });
    } else {
        console.error("Login form not found");
    }
});
